<?
class userAction extends runAction {
	public function _empty() {
		$this->redirect ( MODULE_NAME . '/index' );
	}
	public function mb() {
		
		$F = D ( 'mb' );
		$list = $F->relation ( true )->select ();
		//$list = $F->select ();
		dump ( $list );
	
	}
	public function mb_add() {
		$mb = D ( 'mb' );
		$mb->uname = time ();
		$mb->ct = array ('0' => NULL );
		$mb->dt = array ('0' => NULL );
		$mb->job = array ('0' => NULL );
		$mb->nt = array ('0' => NULL );
		echo $id = $mb->relation ( true )->add ();
	}
	public function mb_edit() {
		$F = D ( 'mb' );
		$data ['uname'] = time ();
		$result = $F->realtion ( true )->save ( $data );
	}
	public function index() {
		
		$F = M ( 'user' );
		
		//$F->field ( 'id,hit,pri,display,sort,title,posttime' );
		

		$sort = $_REQUEST ['sort'];
		if ($sort) {
			$F->where ( "s1 LIKE '%$sort%'" );
		}
		
		echo $kw = $_REQUEST ['kw']=='请输入关键字'?'':$_REQUEST ['kw'];
		if ($kw) {
			$F->where ( "title LIKE '%$kw%'" );
		}
		
		$total = $F->count ();
		
		if ($sort) {
			$F->where ( "s1 LIKE '%$sort%'" );
		}
		if ($kw) {
			$F->where ( "title LIKE '%$kw%'" );
		}
		
		$order = $_REQUEST ['order'];
		if ($order == '') {
			$order = 'id';
		}
		$by = $_REQUEST ['by'];
		if ($by == '') {
			$by = 'DESC';
		}
		$F->order ( "$order $by" );
		
		$ps = $_REQUEST ['ps'];
		if ($ps == '') {
			$ps = '10';
		}
		$F->limit ( $ps );
		
		$p = $_REQUEST ['p'];
		if ($p == '') {
			$p = '1';
		}
		$F->Page ( $p );
		
		$list = $F->select ();
		
		$this->assign ( 'list', $list );
		
		$this->assign ( 'sort', $sort );
		$this->assign ( 'kw', $kw );
		$this->assign ( 'order', $order );
		$this->assign ( 'by', $by );
		$this->assign ( 'p', $p );
		$this->assign ( 'ps', $ps );
		$this->assign ( 'total', $total );
		$this->assign ( 'ptotal', ceil ( $total / $ps ) );
		
		$this->display ( 'index' );
	
	}
	public function del() {
		
		if (count ( $_POST ['id'] ) == 0) {
			echo '没有选中任何记录';
		}
		
		import ( "app.ORG.file" );
		
		//生成 1,2,3
		$in = '';
		$num = 0;
		$dir = 'upload/user/';
		
		foreach ( $_POST ['id'] as $k => $v ) {
			
			if ($v == '') {
				continue;
			}
			
			if ($k != 0) {
				$in .= ',';
			}
			
			$in .= $v;
			$num ++;
			
			//删除附件
			file::del_dir ( $dir . $v );
		}
		
		$F = M ( "user" );
		$F->where ( "id IN ($in)" )->delete ();
		
		$F = M ( "user_info" );
		$F->where ( "id IN ($in)" )->delete ();
		
		//回收ID
		$F = M ( "user" );
		$F->query ( "ALTER TABLE __TABLE__ AUTO_INCREMENT =1" );
		
		$F = M ( "user_info" );
		$F->query ( "ALTER TABLE __TABLE__ AUTO_INCREMENT =1" );
		
		//$this->success ( '已删除 ' . $num . ' 条记录' );
		$this->assign ( 'act_word', '操作成功 : 已删除 ' . $num . ' 条记录' );
		$this->index ();
	}
	public function clean() {
		
		import ( "@.ORG.file" );
		
		$dir = 'upload/user/';
		
		//删除附件
		file::del_dir ( $dir, 0 );
		
		$F = M ( "user" );
		$F->query ( "TRUNCATE TABLE __TABLE__" );
		
		$this->assign ( 'act_word', '清空成功' );
		$this->index ();
		//$this->success ( '清空成功' );
	}
	public function onoff() {
		
		if (count ( $_POST ['id'] ) == 0) {
			
			$this->error ( '没有选中任何记录' );
		}
		if ($_POST ['value'] == '') {
			
			$this->error ( '未指定开关' );
		}
		
		list ( $zd, $onoff ) = explode ( ',', $_POST ['value'] );
		
		$in = '';
		foreach ( $_POST ['id'] as $k => $v ) {
			if ($v != '') {
				if ($k != 0) {
					$in .= ',';
				}
				$in .= $v;
			}
		}
		
		$row [$zd] = $onoff;
		$F = M ( 'user' );
		$F->where ( "id IN ($in)" )->save ( $row );
		
		$this->assign ( 'act_word', '操作成功 : 已更新 ' . mysql_affected_rows () . ' 条记录' );
		$this->index ();
	}
	public function view() {
		
		CK ( 'uploaddir', 'user' );
		
		//import ( "app.ORG.sort" );
		//$s1 = sort::get ( 'user' );
		$this->assign ( 's1', $s1 );
		
		if (empty ( $_GET ['id'] )) {
			
			//add
			$v = array ('hit' => 0, 'pri' => 0, 'display' => 1, 'date' => date ( 'Y-m-d' ), 'time' => date ( 'H:i:s' ) );
			
			$v ['info'] = array ('birthday' => date ( 'Y-m-d' ) );
		} else {
			
			//edit
			$F = M ( 'user' );
			$v = $F->getById ( $_GET ['id'] );
			$v ['date'] = date ( 'Y-m-d', $v [info] ['register'] );
			$v ['time'] = date ( 'H:i:s', $v [info] ['register'] );
			$v [info] ['birthday'] = date ( 'Y-m-d', $v [info] ['birthday'] );
		}
		
		$this->assign ( 'v', $v );
		
		$this->display ();
	}
	public function save() {
		
		import ( 'app.ORG.ztime' );
		
		//$_POST ['register'] = ztime::datetime2line ( $_POST ['date'] . ' ' . $_POST ['time'] );
		//$_POST ['birthday'] = ztime::datetime2line ( $_POST ['birthday'] . ' ' . '00:00:00' );
		
		if ($_POST ['id'] == '') {
			
			//add
			$F = M ( 'user' );
			if ($F->create ()) {
				
				$okey = $_POST ['uname'] . $_POST ['email'] . $_POST ['password'];
				$okey = md5 ( $okey );
				$okey = substr ( $okey, 8, 16 );
				$F->okey = $okey;
				$F->groups = 'admin';
				$F->tag = '系统管理员';
				
				$st = $F->add ();
				//$st = $F->add ();
				if ($st !== false) {
					
					$GLOBALS ['id'] = mysql_insert_id ();
					mk_dir ( 'upload/user/' . $GLOBALS ['id'] );
					
					R ( 'user', 'upload' );
					
					//$this->assign ( 'way', 'close' );
					$this->success ( '添加成功' );
				} else {
					
					$this->error ( '添加失败' );
				}
			
			} else {
				echo $F->getError ();
			}
		} else {
			
			//edit
			$F = M ( 'user' );
			if ($F->create ()) {
				$okey = $_POST ['uname'] . $_POST ['email'] . $_POST ['password'];
				$okey = md5 ( $okey );
				$okey = substr ( $okey, 8, 16 );
				$F->okey = $okey;
				$st = $F->save ();
			}
			
				
			/*$F = D ( 'user_info' );
			if ($F->create ()) {
				$st_info = $F->save ();
			}*/
			
			if ($st !== false && $st_info !== false) {
				
				$GLOBALS ['id'] = $_POST ['id'];
				
				R ( 'user', 'upload' );
				
				//$this->assign ( 'way', 'close' );
				$this->success ( '修改成功' );
			} else {
				
				$this->error ( '修改失败' );
			}
		}
	}
	public function upload() {
		
		//上传通道
		if ($_POST ['upload_way'] == 'old') {
			
			$go = 0;
			foreach ( $_FILES as $k => $v ) {
				if ($k == 'zip') {
					continue;
				}
				if ($v ['size'] != 0) {
					$go = 1;
					break;
				}
			}
			
			if ($go == 1) {
				
				import ( "@.ORG.UploadFile" );
				
				$upload = new UploadFile ();
				//设置上传文件大小
				$upload->maxSize = 5242880;
				//设置上传文件类型
				$upload->allowExts = array ('jpg', 'jpeg', 'gif', 'png', 'zip', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx' );
				//设置附件上传目录
				$upload->savePath = 'upload/user/' . $GLOBALS ['id'] . '/';
				//设置上传文件名
				$upload->saveRule = '';
				//删除原图
				$upload->thumbRemoveOrigin = false;
				
				if (! $upload->upload ()) {
					
					//捕获上传异常
					$this->error ( $upload->getErrorMsg () );
				}
			}
		} elseif ($_POST ['upload_way'] == 'zip') {
			
			if (substr ( strtolower ( $_FILES ['zip'] ['name'] ), - 3 ) == 'zip') {
				
				$dir = RP . 'upload/user/' . $GLOBALS ['id'] . '/';
				$file = $dir . $_FILES ['zip'] ['name'];
				move_uploaded_file ( $_FILES ['zip'] ['tmp_name'], $file );
				
				import ( "@.ORG.zip" );
				
				$z = new unzip ();
				$z->Extract ( $file, $dir );
			
			}
		}
	}
}
?>